/**
 * @description 用户关系的数据库层
 * @author 印明帝
 */
const { SuccessModel, ErrorModel } = require('../model/ResModel')
const { addFollowerFailInfo,deleteFollowerFailInfo } = require('../model/errorInfo')
const { addFollower,deleteFollower,getUsersByFollower,getFollowersByUser } = require('../service/userRelation')


/**
 * 添加关注关系
 * @method follow
 * @param {number} userId 用户 id
 * @param {number} followerId 关注的用户 id
 * @returns {Promise}
 */
async function follow(userId,followerId) {
  let result = await addFollower(userId,followerId)
  if(result){
    return new SuccessModel()
  }
  return new ErrorModel(addFollowerFailInfo)
}

/**
 * 取消关注关系
 * @method unFollow
 * @param {number} userId 用户 id
 * @param {number} followerId 关注的用户 id
 * @returns {Promise}
 */
async function unFollow(userId,followerId) {
  let result = await deleteFollower(userId,followerId)
  if(result){
    return new SuccessModel()
  }
  return new ErrorModel(deleteFollowerFailInfo)
}


/**
  * 获取粉丝列表
  * @method getFans
  * @param {Number} followerId 要获取粉丝的用户id 
  */

async function getFans(followerId) {
  let result = await getUsersByFollower(followerId)
  if(result){
    return new SuccessModel(result)
  }
  return new ErrorModel(deleteFollowerFailInfo)
}


/**
  * 获取关注的人的列表
  * @method getFollowers
  * @param {Number} userId 要获取粉丝的用户id 
  */

async function getFollowers(userId) {
  let result = await getFollowersByUser(userId)
  if(result){
    return new SuccessModel(result)
  }
  return new ErrorModel(deleteFollowerFailInfo)
}


module.exports = {
  follow,
  unFollow,
  getFans,
  getFollowers
}